<template>
  <blog-card-list :blogList="blogList" @delete-blog="deleteBlog"/>
  <van-empty v-show="!blogList || blogList.length < 1" description="您还未点赞过任何一篇博客" />
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import BlogCardList from "../../components/BlogCardList.vue";
import myAxios from "../../plugins/myAxios";
import { showToast } from "vant";

const blogList = ref([]);

const loadData = async () => {
  // 从localStorage获取当前用户信息
  const userJson = localStorage.getItem('user');
  if (!userJson) {
    showToast('请先登录');
    return;
  }
  
  try {
    const currentUser = JSON.parse(userJson);
    
    const res: any = await myAxios.post('/blog/interaction/list', {
      pageNum: 1,
      pageSize: 20,
      type: 4,
      userId: currentUser.id  // 添加用户ID
    });
    if (res?.code === 0) {
      blogList.value = res.data;
    } else {
      showToast('查询失败');
    }
  } catch (error) {
    console.error('解析用户数据失败', error);
    showToast('获取用户信息失败');
  }
};

onMounted(async () => {
  loadData();
});

const deleteBlog = async () => {
  loadData();
};
</script>

